/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
 var postorderTraversal = function(root) {
    // 后续遍历的逻辑和前序遍历的逻辑相似 都是 逐个放入栈中 然后再弹出 只不过是先左后右的放入栈中 这样弹出来才是先右后左
    let res = []
    if(root === null) return res
    let stack = []
    stack.push(root)
    while(stack.length){
      let node = stack.pop()
      res.push(node.val)
      node.left && stack.push(node.left)
      node.right && stack.push(node.right)
    }
    return res.reverse()
};